layui.use(['form', 'table', 'fsoft'], function () {
	var $ = layui.$
		, form = layui.form
		, table = layui.table
		, fsoft = layui.fsoft;
	// 第一个实例
	table.render({
		elem: '#iconTableBody',
		toolbar: '#batchBtnToolBar',
		url: layui.cache['contentPath'] + '/icons/findList',
		cols: [[
			{ type: 'checkbox', fixed: 'left' },
			{ type: 'numbers', fixed: 'left', title: '序号', align: 'center' },
			{ field: 'code', title: '编码', width: 150, sort: true },
			{ field: 'name', title: '名称', width: 200 },
			{
				field: 'className', title: '图标', width: 100, align: 'center', templet: function (item) {
					if (item.iconGroup == 'Font-Awesome')
						return '<i class="' + item.className + '" aria-hidden="true" ></i>';
					else
						return '<i class="' + item.code + ' ' + item.className + '"></i>'
				}
			},
			{ field: 'className', title: '样式名称', width: 300 },
			{ field: 'status', title: '状态', width: 100, templet: '#switchStatus' },
			{ field: 'iconGroup', title: '所属分组', width: 200 },
			{ fixed: 'right', width: 150, align: 'center', title: '操作', toolbar: '#iconTableBar', fixed: 'right' }
		]],
		page: true,
		limit: 20,
		limits: [20, 30, 50, 100, 200],
		height: 'full-108'
	});
	//搜索监听
	form.on('submit(sreach)', function (data) {
		table.reload('iconTableBody', { where: data.field });
		return false;
	});
	//行操作监听 
	table.on('tool(iconTableBody)', function (obj) {
		var data = obj.data;
		if (obj.event === 'edit') { //编辑
			xadmin.open('编辑图标', layui.cache['contentPath'] + '/icons/edit/' + data.id, 450, 500);
		} else if (obj.event === 'del') { //删除
			layer.confirm("确认要删除当前图标？", function (index) {
				$.ajax({
					type: 'POST',
					url: layui.cache['contentPath'] + '/icons/remove',
					data: JSON.stringify([data.id]),
					contentType: "application/json",
					success: function (rs) {
						rs = eval('(' + rs + ')');
						if (rs.code == 0) {
							layer.msg("删除成功", { icon: 1, time: 3000 });
							table.reload('table');
						} else {
							layer.msg(rs.msg, { icon: 2, time: 3000 });
						}
					},
					error: function (status, errors) {
						layer.alert('请求错误，错误码：' + JSON.stringify(status));
					}
				});
			});
		}
	});
	//工具栏监听
	var $ = layui.$, active = {
		removeBatch: function () { // 删除
			var checkStatus = table.checkStatus('table');
			var data = checkStatus.data;
			if (data == undefined || data.length == 0) {
				layer.msg("请至少选择一条记录", { icon: 7, time: 3000 });
				return;
			}
			var ids = new Array();
			for (var k = 0; k < data.length; k++) {
				ids.push(data[k].id);
			}
			layer.confirm("确认要删除已选中的（" + ids.length + "）条图标？", function (index) {
				$.ajax({
					type: 'POST',
					url: layui.cache['contentPath'] + '/icons/remove',
					data: JSON.stringify(ids),
					contentType: "application/json",
					success: function (rs) {
						rs = eval('(' + rs + ')');
						if (rs.code == 0) {
							layer.msg("删除成功", { icon: 1, time: 3000 });
							table.reload('table', { where: data.field });
						} else {
							layer.msg(rs.msg, { icon: 2, time: 3000 });
						}
					},
					error: function (status, errors) {
						layer.alert('请求错误，错误码：' + JSON.stringify(status));
					}
				});
			});
		},
		add: function () {
			xadmin.open('添加图标', layui.cache['contentPath'] + '/icons/add', 450, 500);
		}
	};
	//按钮组监听
	$('.toolbar .layui-btn').on('click', function () {
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

});